package com.example.kuaizu.service.imp;

import com.example.kuaizu.mapper.HoutaiMapper;
import com.example.kuaizu.pojo.PageBean;
import com.example.kuaizu.pojo.UpdateUserInfoParam;
import com.example.kuaizu.pojo.dto.SchoolMajorCountDto;
import com.example.kuaizu.pojo.dto.UpdateAuditStatusDto;
import com.example.kuaizu.pojo.ov.UserProfileAndSchoolOV;
import com.example.kuaizu.pojo.pram.TalentAuditParam;
import com.example.kuaizu.service.InHoutaiService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import kuaizu.common.PageData;
import kuaizu.common.constp.KuaiZuConst;
import kuaizu.common.exception.MassageException;
import kuaizu.common.param.NoAuditProjectParam;
import kuaizu.common.param.QueryTalentParam;
import kuaizu.common.param.StudentInfoListParam;
import kuaizu.common.param.UpdateAuditStatusParam;
import kuaizu.common.tablepojo.Project;
import kuaizu.common.vo.TalentListVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Log4j2
@RequiredArgsConstructor
public class HoutaiService implements InHoutaiService {
    private final HoutaiMapper houtaiMapper;

    //查询学校的学生注册信息
    @Override
    @Transactional
    public PageBean<UserProfileAndSchoolOV> queryStudentInfo(StudentInfoListParam Param) {
        //创建pageBean对象
        PageBean<UserProfileAndSchoolOV> pd = new PageBean<>();
        //开启分夜茶叙 使用分页查询插件 导入坐标  当前页码 ,每页的数据
        PageHelper.startPage(Param.getPageNum(), Param.getPageSize());
        //调用mapper查询
        List<UserProfileAndSchoolOV> uasov = houtaiMapper.selectSchoolStudent(Param);
        //page中提供了方法可以获取pageHelper分页查询的结果 总数据条数,和当前页数据不强转看不到
        Page<UserProfileAndSchoolOV> p = (Page<UserProfileAndSchoolOV>) uasov;
        //把数据填充到pagebean中去
        pd.setItems(p.getResult());//获取指定页的数据
        pd.setTotal(p.getTotal());//获取总条数
        return pd;
    }
    //修改用户学校和专业
    @Transactional
    @Override
    public void updateUserInfo(UpdateUserInfoParam updateUserInfoParam) {
        houtaiMapper.updateUserInfo(updateUserInfoParam);

    }
    @Transactional   //获取注册学生的学校专业人数信息
    @Override
    public  List<SchoolMajorCountDto> getSchoolStudentInfo() {
        List<SchoolMajorCountDto> schoolStudentInfo = houtaiMapper.getSchoolStudentInfo();
        log.info("获取注册学生的学校专业人数信息:{}",schoolStudentInfo);
        return schoolStudentInfo;
    }
    @Transactional
    @Override
    public void updateAuditStatus(UpdateAuditStatusParam param) {
        //修改数据库项目的审核状态
        UpdateAuditStatusDto dto = new UpdateAuditStatusDto();
        BeanUtils.copyProperties(param,dto);
        if(param.getAuditStatus()){
            dto.setAuditStatus(KuaiZuConst.PROJECT_CHECK_PASS);
        }else{
            dto.setAuditStatus(KuaiZuConst.PROJECT_CHECK_NO);
        }
        houtaiMapper.updateAuditStatus(dto);
    }

    //获取未审核的项目
    @Override
    @Transactional
    public Page<Project> getNoAuditProject(NoAuditProjectParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Project> noAuditProject = houtaiMapper.getNoAuditProject();
        Page<Project> page = (Page<Project>) noAuditProject;
        return page;
    }

    //人才审核
    @Override
    @Transactional
    public int talentAudit(TalentAuditParam param) {
        return houtaiMapper.talentAudit(param);
    }

    @Override
    @Transactional
    public PageData<TalentListVo> queryTalentInfo(QueryTalentParam param) {
        PageMethod.startPage(param.getPageNum(), param.getPageSize());
        List<TalentListVo> list = new ArrayList<>();
        PageData<TalentListVo> page = new PageData<>();
        try {
            list = houtaiMapper.queryTalentInfo(param);
        } finally {
            PageMethod.clearPage();
        }
        if (CollectionUtils.isEmpty(list)) {
            throw new MassageException("没有数据");
        }
        PageInfo<TalentListVo> pageIndo = new PageInfo<>(list);
        page.setTotal(pageIndo.getTotal());
        page.setPageNum(pageIndo.getPageNum());
        page.setPageSize(pageIndo.getPageSize());
        page.setData(pageIndo.getList());
        return page;
    }


}
